%% Produce Figure 2

addpath('functions');
addpath('data');

load('output\results_Figure2')
load('recession.mat');

ydata=yraw;

v2struct(settings);
v2struct(results);

%% Colour Scheme

% Coloured Figures

red = [1 0 0];
blue = [0 0 1];

% Black and White Figures

blue = [0.2 0.2 0.2];
red = [0 0 0];


%% Figure 2a - Posterior Estimate of the Trend

annualise = 1;

trend = means(1) + (1+annualise*2)*(1+standardise*(stdevs(1)-1))*nanmean(Draws_Factors(:,1,:),3);
trend_percentiles = means(1) + (1+annualise*2)*(1+standardise*(stdevs(1)-1))*prctile(squeeze(Draws_Factors(:,1,:)),[5,16,50,84,95],2);

f = figure(1);
    
bar(dates(p+1:end),5.98.*recession(p+1:p+size(dates(p+1:end),1)),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1)
hold on

plot(dates(p+1:end),trend_percentiles(:,3),'color',red,'LineWidth',2.5)
plot(dates(p+1:end),trend_percentiles(:,[2,4]),'LineStyle',':','color',blue,'LineWidth',1.5)
plot(dates(p+1:end),trend_percentiles(:,[1,5]),'LineStyle','--','color',blue,'LineWidth',1.5)

hold off

datesToPlot = datenum(1950:10:2020,1,1);
YAxisTicks = [0:1:5];
set(gca,'XTick',datesToPlot,'GridLineStyle',':','YTick',YAxisTicks)
datetick('x','yyyy','keepticks')
xmin=datenum(1947,1,1);
xmax=datenum(2015,6,1);
xlim([xmin,xmax])
ymin = 0;
ymax = 5;
ylim([ymin,ymax])
grid on

figSize = [10 5];
set(f, 'PaperUnits', 'inches');
set(f, 'Units','inches');
set(f, 'PaperSize', figSize);
set(f, 'PaperPositionMode', 'auto');
set(f, 'Position', [0 0 figSize(1) figSize(2)])
set(gca, 'FontName', 'Times-Roman');
set(gca, 'FontSize', 20);
set(gca, 'Layer', 'Top');

% set(gca,'OuterPosition',[-0.12 -.06 1.2 1.1]);

ti = get(gca,'TightInset');
set(gca,'Position',[1.1*ti(1) 0.9*ti(2) 0.99*(1-ti(3)-ti(1)) 0.99*(1-ti(4)-ti(2))]);
pos = get(gca,'Position');
ti = get(gca,'TightInset');

fname = strcat('output\Figure2a.pdf');
print('-dpdf', f, fname);

%% Figure 2b - In-Sample Fit of GDP

annualise = 1;

mean_trend = means(1) + (1+annualise*2)*(1+standardise*(stdevs(1)-1))*nanmean(Draws_Factors(:,1,:),3);
mean_cycle = (1+standardise*(stdevs(1)-1))*nanmean(Draws_Factors(:,2,:),3);
mean_cycle = [mean_cycle mlag2(mean_cycle,4)]*[1/3 2/3 1 2/3 1/3]';
interpolated_hours = interpolate(ydata(q+1:end,3));
interpolated_gdp = interpolate(ydata(q+1:end,1));
interpolated_consumption = interpolate(ydata(q+1:end,2));

f = figure(2);
    
bar(dates(p+1:end),16.*recession(p+1:size(dates,1)),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1)
hold on
bar(dates(p+1:end),-12.9.*recession(p+1:size(dates,1)),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1)
plot(dates(p+1:end),interpolated_gdp(:,1),'color',blue,'LineWidth',1)
% plot(dates(p+1:end),mean_trend(:,1),':r','LineWidth',2.5)
plot(dates(p+1:end),means(1)+mean_cycle(:,1),'color',red,'LineWidth',2.5)
hold off

datesToPlot = datenum(1950:10:2020,1,1);
YAxisTicks = [-12:4:16];
set(gca,'XTick',datesToPlot,'GridLineStyle',':','YTick',YAxisTicks)
datetick('x','yyyy','keepticks')
xmin=datenum(1947,1,1);
xmax=datenum(2015,6,1);
xlim([xmin,xmax])
ymin = -12;
ymax = 16;
ylim([ymin,ymax])
grid on

figSize = [10 5];
set(f, 'PaperUnits', 'inches');
set(f, 'Units','inches');
set(f, 'PaperSize', figSize);
set(f, 'PaperPositionMode', 'auto');
set(f, 'Position', [0 0 figSize(1) figSize(2)])
set(gca, 'FontName', 'Times-Roman');
set(gca, 'FontSize', 20);
set(gca, 'Layer','top');
% set(gca,'OuterPosition',[-0.12 -.06 1.2 1.1]);

ti = get(gca,'TightInset');
set(gca,'Position',[1.1*ti(1) 1.1*ti(2) 0.99*(1-ti(3)-ti(1)) 0.99*(1-ti(4)-ti(2))]);

fname = strcat('output\Figure2b.pdf');
print('-dpdf', f, fname);

%% Figure 2c - Posterior Estimate of the Volatility

annualise = 1;

sd_percentiles = results.sd_percentiles;

f = figure(3);

bar(dates(p+1:end),7.97.*recession(p+1:size(dates,1)),'FaceColor',[0.9 0.9 0.9],'EdgeColor',[0.9 0.9 0.9],'LineWidth',0.1)
hold on
plot(dates(p+1:end),sd_percentiles(:,3),'color',red,'LineWidth',2.5)
plot(dates(p+1:end),sd_percentiles(:,[2,4]),'LineStyle',':','color',blue,'LineWidth',1.5)
plot(dates(p+1:end),sd_percentiles(:,[1,5]),'LineStyle','--','color',blue,'LineWidth',1.5)
hold off

datesToPlot = datenum(1950:10:2020,1,1);
YAxisTicks = [0:1:8];
set(gca,'XTick',datesToPlot,'GridLineStyle',':','YTick',YAxisTicks)
datetick('x','yyyy','keepticks')
xmin=datenum(1947,1,1);
xmax=datenum(2015,6,1);
xlim([xmin,xmax])
ymin = 0;
ymax = 8;
ylim([ymin,ymax])
grid on

figSize = [10 5];
set(f, 'PaperUnits', 'inches');
set(f, 'Units','inches');
set(f, 'PaperSize', figSize);
set(f, 'PaperPositionMode', 'auto');
set(f, 'Position', [0 0 figSize(1) figSize(2)])
set(gca, 'FontName', 'Times-Roman');
set(gca, 'FontSize', 20);
set(gca, 'Layer', 'Top');

% set(gca,'OuterPosition',[-0.12 -.06 1.2 1.1]);

ti = get(gca,'TightInset');
set(gca,'Position',[1.1*ti(1) 1.1*ti(2) 0.99*(1-ti(3)-ti(1)) 0.99*(1-ti(4)-ti(2))]);
pos = get(gca,'Position');
ti = get(gca,'TightInset');
% 
fname = strcat('output\Figure2c.pdf');

print('-dpdf', f, fname);

